// // 微信阅读  美添赚
var pn = 'com.tencent.mm'



upapp(0)
taskCenter()
SignIning()
tx(11031140, 8888, 2)  //一1959513
readnew()
// vi(5)
// read(5)
exapp()




// gzhgz()
// vilook()


// // 启动app
function upapp(n) {
    toastLog("开始启动-微信")
    home()
    sleep(random(3000, 5000))
    launchApp("微信")
    sleep(random(3000, 5000))
    if (text("使用以下方式打开").exists()) {
        var like = text("微信").findOnce().parent().parent().child(n)
        randomClick(like)
        sleep(random(3000, 5000))
    }
    // 启动一个新线程
    threads.start(function () {
        while (true) {
            var mypk = currentPackage()
            if (mypk != pn) {
                launchApp("微信")
                sleep(random(3000, 5000))
                if (text("使用以下方式打开").exists()) {
                    var like = text("微信").findOnce().parent().parent().child(n)
                    randomClick(like)
                    sleep(random(3000, 5000))
                }
            }
            //text("网络错误")
            if (text("网络错误").exists()) {
                var like = text("重试").findOnce()
                randomClick(like)
                toastLog("点击重试/新线程")
                sleep(random(3000, 5000))
            }
            sleep(2000)
        }
    })
    // 确认微信启动成功
    if (!id("nvt").exists() || !text("互助活动").exists()) {
        id("nvt").findOne(20000) || text("互助活动").findOne(20000)
        if (!id("nvt").exists() || !text("互助活动").exists()) {
            for (var i = 0; i < 6; i++) {
                if (id("nvt").exists()) {
                    toastLog("发现微信主页面")
                    break
                }
                if (text("互助活动").exists()) {
                    toastLog("发现美添赚主页面")
                    var like = text("互助活动").findOnce().parent()
                    randomClick(like)
                    sleep(random(3000, 5000))
                    break
                }
                back()
                toastLog("未在主页面")
                sleep(random(3000, 5000))
            }
        }
        toastLog("微信启动完成")
        sleep(random(3000, 5000))
    }
}

// //收藏夹阅读
function taskCenter() {
    try {
        // 确认微信启动成功
        if (!id("nvt").exists() || !text("互助活动").exists()) {
            id("nvt").findOne(20000) || text("互助活动").findOne(20000)
            if (!id("nvt").exists() || !text("互助活动").exists()) {
                for (var i = 0; i < 6; i++) {
                    if (id("nvt").exists()) {
                        toastLog("发现微信主页面")
                        break
                    }
                    if (text("互助活动").exists()) {
                        toastLog("发现美添赚主页面")
                        var like = text("互助活动").findOnce().parent()
                        randomClick(like)
                        sleep(random(3000, 5000))
                        break
                    }
                    back()
                    toastLog("未在主页面")
                    sleep(random(3000, 5000))
                    sleep(random(3000, 5000))
                }
            }
            toastLog("微信启动完成")
            sleep(random(3000, 5000))
        }
        if (text("互助活动").exists()) {
            toastLog("发现美添赚主页面")
            var like = text("互助活动").findOnce().parent()
            randomClick(like)
            sleep(random(3000, 5000))
        } else {
            let maxRetry = 5;
            while (!id("nvt").exists() && maxRetry-- > 0) {
                toastLog("返回微信主页面")
                back()
                sleep(random(3000, 5000))
            }
            // //我
            if (id("nvt").exists()) {
                toastLog("发现微信主页面")
                var like = id("nvt").findOnce(3)
                randomClick(like);
                sleep(random(3000, 5000))

                // //收藏夹
                if (id("m7k").indexInParent("4").exists()) {
                    toastLog("进入收藏夹")
                    var like = id("m7k").indexInParent("4").findOnce()
                    randomClick(like);
                    sleep(random(3000, 5000))

                    if (className("android.widget.ListView").exists()) {
                        toastLog("确保在我的收藏第一个位置")
                        var like = className("android.widget.ListView").findOnce().child(0)
                        // var like = boundsInside(0,800,1080,960).clickable(true).findOnce()
                        randomClick(like);
                        sleep(random(3000, 5000))
                        // 详情//来自

                        if (className("android.widget.ImageView").longClickable(true).exists()) {
                            toastLog("识别图片")
                            // bounds("(65,229,1015,1648)")
                            longClick(device.width * 0.2, device.height * 0.3, device.width * 0.75, device.height * 0.7);
                            sleep(random(3000, 5000))
                            if (longClickable(true).exists()) {
                                longClick(device.width * 0.2, device.height * 0.3, device.width * 0.75, device.height * 0.7);
                                if (longClickable(true).exists()) {
                                    longClick(device.width * 0.2, device.height * 0.3, device.width * 0.75, device.height * 0.7);
                                    sleep(random(3000, 5000))
                                }
                            }

                            text("识别图中的二维码").findOne(20000)
                            sleep(random(3000, 5000))
                            // text("识别图中的二维码")
                            if (className("android.widget.LinearLayout").clickable(true).exists()) {
                                var n = className("android.widget.LinearLayout").clickable(true).find().size()
                                var like = className("android.widget.LinearLayout").clickable(true).findOnce((n - 2))
                                randomClick(like)
                                sleep(random(3000, 5000))
                                text("继续访问").findOne(20000)
                                sleep(random(3000, 5000))
                                // text("已停止访问该网页")text("经网址安全检测，该网页包含不安全内容。为维护绿色上网环境，已停止访问。")
                                if (text("已停止访问该网页").exists()) {
                                    toastLog("该网页包含不安全内容")
                                    back()
                                    sleep(random(3000, 5000))
                                }
                                if (text("如需浏览，请长按网址复制后使用浏览器访问").exists()) {
                                    sleep(random(3000, 5000))
                                    back()
                                    sleep(random(3000, 5000))
                                    sleep(random(3000, 5000))
                                }
                            }
                        }
                    }
                }
            }

            // text("继续访问")text("非微信官方网页，请确认是否继续访问。")
            if (text("继续访问").exists()) {
                toastLog("继续访问")
                sleep(random(3000, 5000))
                var like = text("继续访问").findOnce()
                if (like.bounds().bottom >= 1900) {
                    upsm(1)
                }
                var like = text("继续访问").findOnce()
                randomClick(like);
                text("同意并继续使用").findOne(20000)
                sleep(random(3000, 5000))

                // text("《美添赚用户协议》")text("同意并继续使用")
                if (text("同意并继续使用").exists()) {
                    toastLog("《美添赚用户协议》")
                    var like = text("同意并继续使用").findOnce()
                    if (like.bounds().bottom >= 1900) {
                        upsm(1)
                    }
                    var like = text("同意并继续使用").findOnce()
                    randomClick(like)
                    sleep(random(3000, 5000))
                    if (text("您已同意协议内容").exists()) {
                        toastLog("您已同意协议内容")
                        var like = text("确定").findOnce()
                        randomClick(like)
                        sleep(random(3000, 5000))
                    }
                }
                while (text("继续访问").exists()) {
                    toastLog("网页未跳转,点击继续访问")
                    sleep(random(3000, 5000))
                    var like = text("继续访问").findOnce()
                    if (like.bounds().bottom >= 1900) {
                        upsm(1)
                    }
                    var like = text("继续访问").findOnce()
                    randomClick(like);
                    sleep(random(3000, 5000))
                }
                if (text("如需浏览，请长按网址复制后使用浏览器访问").exists()) {
                    toastLog("如需浏览")
                    sleep(random(3000, 5000))
                    back()
                    sleep(random(3000, 5000))
                    sleep(random(3000, 5000))
                }
                if (text("即将前往以下网址，无法确认本次跳转是否安全，请使用浏览器访问。").exists()) {
                    toastLog("即将前往以下网址")
                    sleep(random(3000, 5000))
                    back()
                    sleep(random(3000, 5000))
                    sleep(random(3000, 5000))
                }
            }
            // text("《美添赚用户协议》")text("同意并继续使用")
            if (text("同意并继续使用").exists()) {
                toastLog("《美添赚用户协议》")
                var like = text("同意并继续使用").findOnce()
                randomClick(like)
                sleep(random(3000, 5000))
                if (text("您已同意协议内容").exists()) {
                    toastLog("您已同意协议内容")
                    var like = text("确定").findOnce()
                    randomClick(like)
                    sleep(random(3000, 5000))
                }
            }

            // text("小来福的小胖虎 申请")text("使用其他头像和昵称")text("你的昵称、头像")text("允许")
            if (text("你的昵称、头像").exists()) {
                toastLog("使用头像和昵称")
                var like = text("允许").findOnce()
                randomClick(like);
                sleep(random(3000, 5000))
                text("互助活动").findOne(20000)
                sleep(random(3000, 5000))
            }

            // 重新授权
            if (text("使用完整服务").exists()) {
                var like = text("使用完整服务").findOnce().parent()
                randomClick(like);
                sleep(random(3000, 5000))
                if (text("允许").exists()) {
                    var like = text("允许").findOnce()
                    randomClick(like);
                    sleep(random(3000, 5000))
                }
            }

            // text("已停止访问该网页")
            if (text("已停止访问该网页").exists()) {
                var like = desc("返回").findOnce()
                randomClick(like);
                sleep(random(3000, 5000))
                taskCenter()
            }
            // 识别不到《美添赚用户协议》
            if (!text("互助活动").exists()) {
                toastLog("未识别到-协议")
                var randX = random(199, 880);
                var randY = random(966, 1103);
                click(randX, randY)
                sleep(random(3000, 5000))
            }
            while (text("游客").exists()) {
                toastLog("正在加载，请稍等。。。")
                sleep(random(3000, 5000))
                sleep(random(3000, 5000))
                if (text("游客").exists()) {
                    var like = desc("更多信息").findOnce()
                    randomClick(like)
                    sleep(random(3000, 5000))
                    var like = text("刷新").findOnce().parent()
                    randomClick(like)
                    sleep(random(3000, 5000))
                }
            }

            text("公告通知").findOne(10000)
            // text("美添赚公告通知")
            if (text("公告通知").exists()) {
                toastLog("发现通知")
                sleep(random(8000, 10000))
                var like = text("公告通知").findOnce().parent().findOne(text("我知道了"))
                if (like.bounds().bottom > 1800) {
                    upsm(1)
                }
                var like = text("公告通知").findOnce().parent().findOne(text("我知道了"))
                randomClick(like)
                sleep(random(3000, 5000))
            }
        }
        //text("互助活动") bounds("(467,1813,612,1957)"),text("互助活动")bounds("(432,1867,648,2032)")
        if (text("互助活动").exists()) {
            var like = text("互助活动").findOnce().parent()
            randomClick(like)
            var n = textStartsWith("用户ID").findOnce().indexInParent()
            toastLog("用户：" + textStartsWith("用户ID").findOnce().parent().child((n - 1)).text())
            sleep(random(3000, 5000))
        } else {
            sleep(random(3000, 5000))
            sleep(random(3000, 5000))
        }
        // text("继续访问")text("非微信官方网页，请确认是否继续访问。")
        if (text("继续访问").exists()) {
            toastLog("继续访问")
            sleep(random(3000, 5000))
            var like = text("继续访问").findOnce()
            if (like.bounds().bottom >= 1900) {
                upsm(1)
            }
            var like = text("继续访问").findOnce()
            randomClick(like);
            sleep(random(3000, 5000))
            while (text("继续访问").exists()) {
                toastLog("网页未跳转,继续点击")
                sleep(random(10000, 15000))
                var like = text("继续访问").findOnce()
                if (like.bounds().bottom >= 1900) {
                    upsm(1)
                }
                var like = text("继续访问").findOnce()
                randomClick(like);
                sleep(random(1000, 3000))
            }
        }

    } catch (error) {
        toastLog("任务中心异常错误,未完成")
        toastLog(error)
        sleep(random(3000, 5000))
    }

}


// text("我的主页")// text("已领取")
// text("签到积分根据昨日活动获得的积分领取5～60积分(活动积分越多,领取积分越多)")
function SignIning() {
    try {
        if (!text("互助活动").exists()) {
            toastLog("未发现互助，重启任务中心")
            taskCenter()
        }
        if (text("我的主页").exists()) {
            toastLog("开始签到")
            var like = text("我的主页").findOnce().parent()
            randomClick(like)
            sleep(random(3000, 5000))
            if (textStartsWith("签到").exists()) {
                var n = textStartsWith("签到").findOnce().indexInParent()
                if (textStartsWith("签到").findOnce().parent().child((n + 1)).child(0).text() == "点击领取") {
                    var like = textStartsWith("签到").findOnce().parent().child((n + 1))
                    randomClick(like)
                    sleep(random(3000, 5000))
                    toastLog("签到完成")
                    regen()
                }
            }
        }
        // bounds("(359,1924,720,2032)")bounds("(423,1916,656,2032)")
        // bounds("(432,1867,648,2032)")
        // var randX = random(435, 645);
        // var randY = random(1925, 2030);
        // click(randX, randY)
        // sleep(random(3000, 5000))
        if (text("互助活动").exists()) {
            toastLog("发现互助活动")
            var like = text("互助活动").findOnce().parent()
            randomClick(like)
            sleep(random(3000, 5000))
        }
    } catch (error) {
        toastLog("签到异常错误,未完成")
        sleep(random(3000, 5000))
    }
}

// text("文章阅读推荐")
function readnew() {
    try {
        toastLog("开始文章阅读推荐任务")
        sleep(random(3000, 5000))
        sleep(random(3000, 5000))
        if (!text("文章阅读推荐").exists()) {
            var like = text("互助活动").findOnce().parent()
            randomClick(like)
            text("文章阅读推荐").findOne(10000)
            sleep(random(3000, 5000))
        }
        if (!text("文章阅读推荐").exists()) {
            toastLog("未发现互助，重启任务中心")
            taskCenter()
        }
        if (text("文章阅读推荐").exists()) {
            var t = text("文章阅读推荐").findOnce().parent().child(5)
            if (t.bounds().bottom > 1800) {
                upsm(1)
            }
            if (t.text() != "开始活动") {
                toastLog("文章阅读推荐" + t.text())
                sleep(random(3000, 5000))
            }
            if (t.text() == "开始活动") {
                toastLog("文章阅读推荐")
                var like = text("文章阅读推荐").findOnce().parent().child(5)
                randomClick(like);
                text("点击复制广告").findOne(20000)
                sleep(random(3000, 5000))
                if (!text("点击复制广告").exists()) {
                    toastLog("文章阅读推荐1")
                    var like = text("文章阅读推荐").findOnce().parent().child(5)
                    randomClick(like);
                    sleep(random(3000, 5000))
                    if (!text("点击复制广告").exists()) {
                        toastLog("文章阅读推荐2")
                        var like = text("文章阅读推荐").findOnce().parent().child(5)
                        randomClick(like);
                        sleep(random(3000, 5000))
                    }
                }


                if (!text("点击复制广告").exists()) {
                    toastLog("文章阅读推荐1")
                    var like = text("文章阅读推荐").findOnce().parent().child(5)
                    randomClick(like);
                    text("点击复制广告").findOne(20000)
                    sleep(random(3000, 5000))
                    if (!text("点击复制广告").exists()) {
                        toastLog("文章阅读推荐2")
                        var like = text("文章阅读推荐").findOnce().parent().child(5)
                        randomClick(like);
                        text("点击复制广告").findOne(20000)
                        sleep(random(3000, 5000))
                    }
                }
                if (text("点击复制广告").exists()) {
                    var like = text("点击复制广告").findOnce()
                    randomClick(like);
                    sleep(random(3000, 5000))
                    sleep(random(3000, 5000))
                } else {
                    var t = text("文章阅读推荐").findOnce()
                    toastLog("可能暂时没有任务")
                    sleep(random(3000, 5000))
                }
                // 点击二维码
                if (text("务必观看视频教程").exists()) {
                    var n = text("务必观看视频教程").findOnce().indexInParent()
                    var like = text("务必观看视频教程").findOnce().parent().child((n - 1)).child(0)
                    longCk(like)
                    text("前往图中包含的公众号").findOne(20000)
                    sleep(random(3000, 5000))
                }

                // text("前往图中包含的公众号")
                if (className("android.widget.LinearLayout").clickable(true).exists()) {
                    var n = className("android.widget.LinearLayout").clickable(true).find().size()
                    var like = className("android.widget.LinearLayout").clickable(true).findOnce((n - 2))
                    randomClick(like)
                    sleep(random(3000, 5000))
                    text("青团设计").findOne(20000)
                    sleep(random(3000, 5000))
                }

                //xx未更新  text("关注公众号")青团设计公众号
                if (text("关注公众号").exists()) {
                    var like = text("关注公众号").findOnce().parent()
                    randomClick(like);
                    sleep(random(3000, 5000))
                    if (text("发消息").exists()) {
                        var like = text("发消息").findOnce().parent()
                        randomClick(like);
                        sleep(random(3000, 5000))
                    }
                }

                // 粘贴链接clickable("true")
                if (className("android.widget.EditText").exists()) {
                    var like = className("android.widget.EditText").findOnce()
                    randomClick(like);
                    sleep(random(2000, 3000))
                    paste()
                    sleep(random(2000, 3000))
                    // text("发送")
                    while (!className("android.widget.Button").exists()) {
                        toastLog("重新粘贴")
                        paste()
                        sleep(random(3000, 5000))
                    }
                }
                // text("发送")
                if (className("android.widget.Button").exists()) {
                    var like = className("android.widget.Button").findOnce()
                    randomClick(like);
                    sleep(random(3000, 5000))
                }
                // text("点击链接开始阅读")fullId("com.tencent.mm:id/bkl")clickable("true")className("android.widget.TextView")
                if (className("android.widget.TextView").clickable(true).exists()) {
                    // className("android.widget.TextView").clickable(true)
                    toastLog("点击链接开始阅读")
                    var like1 = className("android.widget.TextView").clickable(true).find()
                    var like = like1[like1.size() - 1]
                    // randomClick(like)
                    var randX = random(like.bounds().left + 30, like.bounds().right - 48);
                    var randY = random(like.bounds().top + 24, like.bounds().bottom - 26);
                    // toastLog(randX + "+" + randY)
                    click(randX, randY)
                    sleep(random(3000, 5000))
                    sleep(random(3000, 5000))
                    // text("微信号:  es20220320")
                    if (textStartsWith("微信号").exists()) {
                        toastLog("跳转到微信资料,开始返回")
                        back()
                        sleep(random(1000, 3000))
                    }
                    if (text("已关注公众号").exists()) {
                        toastLog("跳转到公众号,开始返回")
                        back()
                        sleep(random(1000, 3000))
                    }

                    while (textEndsWith("开始阅读").exists()) {
                        toastLog("阅读链接未点中")
                        var like1 = textEndsWith("开始阅读").find()
                        var like = like1[like1.size() - 1]
                        // randomClick(like)
                        var randX = random(like.bounds().left + 30, like.bounds().right - 48);
                        var randY = random(like.bounds().top + 24, like.bounds().bottom - 26);
                        // toastLog(randX + "+" + randY)
                        click(randX, randY)
                        sleep(random(3000, 5000))
                        sleep(random(3000, 5000))
                        // text("微信号:  es20220320")
                        if (textStartsWith("微信号").exists()) {
                            toastLog("跳转到微信资料,开始返回")
                            back()
                            sleep(random(1000, 3000))
                        }
                        if (text("已关注公众号").exists()) {
                            toastLog("跳转到公众号,开始返回")
                            back()
                            sleep(random(1000, 3000))
                        }
                    }

                    // text("继续访问")text("非微信官方网页，请确认是否继续访问。")
                    if (text("继续访问").exists()) {
                        toastLog("等待5秒,继续访问")
                        sleep(random(3000, 5000))
                        var like = text("继续访问").findOnce()
                        if (like.bounds().bottom >= 1900) {
                            upsm(1)
                        }
                        var like = text("继续访问").findOnce()
                        randomClick(like);
                        sleep(random(3000, 5000))
                        while (text("继续访问").exists()) {
                            toastLog("网页未跳转,继续点击")
                            sleep(random(10000, 15000))
                            var like = text("继续访问").findOnce()
                            if (like.bounds().bottom >= 1900) {
                                upsm(1)
                            }
                            var like = text("继续访问").findOnce()
                            randomClick(like);
                            sleep(random(1000, 3000))
                        }
                    }

                    //阅读文章篇数
                    for (ii = 0; ii < 30; ii++) {
                        sleep(random(3000, 5000))

                        // text("已停止访问该网页") 返回到开始阅读
                        if (text("已停止访问该网页").exists()) {
                            toastLog("已停止访问该网页")
                            var like = desc("返回").findOnce().parent()
                            randomClick(like)
                            sleep(random(3000, 5000))
                            while (!textEndsWith("开始阅读").exists()) {
                                back()
                                sleep(random(3000, 5000))
                            }
                            while (textEndsWith("开始阅读").exists()) {
                                toastLog("阅读链接未点中")
                                var like1 = textEndsWith("开始阅读").find()
                                var like = like1[like1.size() - 1]
                                // randomClick(like)
                                var randX = random(like.bounds().left + 30, like.bounds().right - 48);
                                var randY = random(like.bounds().top + 24, like.bounds().bottom - 26);
                                // toastLog(randX + "+" + randY)
                                click(randX, randY)
                                sleep(random(3000, 5000))
                                sleep(random(3000, 5000))
                                // text("微信号:  es20220320")
                                if (textStartsWith("微信号").exists()) {
                                    toastLog("跳转到微信资料,开始返回")
                                    back()
                                    sleep(random(1000, 3000))
                                }
                                if (text("已关注公众号").exists()) {
                                    toastLog("跳转到公众号,开始返回")
                                    back()
                                    sleep(random(1000, 3000))
                                }
                            }

                        }

                        if (text("继续访问").exists()) {
                            toastLog("网页跳出,继续访问")
                            sleep(random(3000, 5000))
                            var like = text("继续访问").findOnce()
                            if (like.bounds().bottom >= 1900) {
                                upsm(1)
                            }
                            var like = text("继续访问").findOnce()
                            randomClick(like);
                            sleep(random(3000, 5000))
                            while (text("继续访问").exists()) {
                                toastLog("网页未跳转,继续点击")
                                sleep(random(10000, 15000))
                                var like = text("继续访问").findOnce()
                                if (like.bounds().bottom >= 1900) {
                                    upsm(1)
                                }
                                var like = text("继续访问").findOnce()
                                randomClick(like);
                                sleep(random(1000, 3000))
                            }
                        }
                        if (textStartsWith("获取中请稍后").exists()) {
                            let maxRetry = 5;
                            while (textStartsWith("获取中请稍后").exists() && maxRetry-- > 0) {
                                toastLog("获取中请稍后...")
                                sleep(random(3000, 5000))
                            }
                            if (textStartsWith("获取中请稍后").exists()) {
                                regen()
                            }
                            if (text("继续访问").exists()) {
                                toastLog("网页跳出,继续访问")
                                sleep(random(3000, 5000))
                                var like = text("继续访问").findOnce()
                                if (like.bounds().bottom >= 1900) {
                                    upsm(1)
                                }
                                var like = text("继续访问").findOnce()
                                randomClick(like);
                                sleep(random(1000, 3000))
                            }
                        }
                        if (textContains("/").exists() && text("兴趣类文章阅读").exists()) {
                            let maxRetry = 5;
                            while (textContains("/").exists() && text("兴趣类文章阅读").exists() && maxRetry-- > 0) {
                                toastLog("卡住20秒//////")
                                sleep(random(3000, 5000))
                            }
                            if (textContains("/").exists() && text("兴趣类文章阅读").exists()) {
                                regen()
                            }
                            if (text("继续访问").exists()) {
                                toastLog("网页跳出,继续访问")
                                sleep(random(3000, 5000))
                                var like = text("继续访问").findOnce()
                                if (like.bounds().bottom >= 1900) {
                                    upsm(1)
                                }
                                var like = text("继续访问").findOnce()
                                randomClick(like);
                                sleep(random(1000, 3000))
                            }
                        }

                        // text("关注").findOne(20000)
                        text("阅读").findOne(20000)
                        sleep(random(3000, 5000))
                        // text("微信阅读")//text("已完成")

                        if (textStartsWith("系统检测到你已失效").exists()) {
                            toastLog("系统检测到你已失效")
                            var like = text("我知道了").findOnce()
                            randomClick(like)
                            sleep(random(3000, 5000))
                        }

                        if (text("已完成").exists()) {
                            toastLog("文章阅读推荐任务完成")
                            sleep(random(3000, 5000))
                            while (!text("互助活动").exists() && !id("nvt").exists()) {
                                toastLog("已经完成，返回互助活动")
                                back()
                                sleep(random(3000, 5000))
                            }
                            break
                        }

                        if (text("20230906001").exists()) {
                            toastLog("阅读失效了")
                            var like = text("20230906001").findOnce()
                            randomClick(like)
                            sleep(random(3000, 5000))
                        }

                        // text("请返回")阅读超时
                        if (text("请返回").exists()) {
                            toastLog("阅读超时,请返回")
                            var like = desc("返回").findOnce()
                            randomClick(like)
                            sleep(random(3000, 5000))
                            while (!text("互助活动").exists()) {
                                toastLog("未发现互助活动")
                                back()
                                sleep(random(3000, 5000))
                                sleep(random(3000, 5000))
                            }
                            regen()
                            break
                        }

                        if (text("互助活动").exists()) {
                            toastLog("发现主页")
                            regen()
                            var t = text("文章阅读推荐").findOnce().parent().child(6)
                            break
                        }

                        for (i = 0; i < 2; i++) {
                            sml_move(device.width / 2, device.height * 0.7, device.width / 2, device.height * 0.3, 500);
                            sleep(random(2000, 3000));
                        }
                        back();
                        toastLog("第" + (ii + 1) + "次阅读完成")
                    }

                    while (!text("文章阅读推荐").exists()) {
                        regen()
                    }
                }
                if (text("文章阅读推荐").exists()) {
                    var t = text("文章阅读推荐").findOnce().parent().child(5)
                    if (t.text() != "开始活动") {
                        toastLog("文章阅读推荐" + t.text())
                        sleep(random(3000, 5000))
                    }
                }
            }
        }
    } catch (error) {
        toastLog("文章阅读异常错误,未完成")
        toastLog(error)
        sleep(random(3000, 5000))
    }
    // 用户名 关注
    // if (textStartsWith("用户ID").exists()) {
    //     var n = textStartsWith("用户ID").findOnce().indexInParent()
    //     var username = textStartsWith("用户ID").findOnce().parent().child((n - 1)).text().match(/\d+/g)
    //     if (username == null) {
    //         toastLog("用户名符合条件，开始关注")
    //         gzhgz()
    //         vilook()
    //     } else {
    //         toastLog("用户名不符合条件，不能关注")
    //     }
    // }
}


// text("公众号关注推荐")
function gzhgz() {
    for (var i = 0; i < 20; i++) {
        try {
            for (var ii = 0; ii < 2; ii++) {
                // "关注未点中"
                if (text("本次已关注").exists()) {
                    toastLog("发现关注公众号按钮")
                    var like = text("本次已关注").findOnce()
                    randomClick(like);
                    sleep(random(3000, 5000))
                    sleep(random(3000, 5000))
                    break
                }
                if (!text("互助活动").exists()) {
                    toastLog("没发现互助活动")
                    back()
                    sleep(random(3000, 5000))
                    sleep(random(3000, 5000))
                    if (text("互助活动").exists()) {
                        toastLog("点击互助活动")
                        var like = text("互助活动").findOnce().parent()
                        randomClick(like)
                        sleep(random(3000, 5000))
                    }
                    if (!text("互助活动").exists()) {
                        toastLog("重启 任务中心")
                        taskCenter()
                    }
                }
                if (text("互助活动").exists()) {
                    break
                }
            }
            if (!text("公众号关注推荐").exists()) {
                toastLog("重新启动任务中心")
                taskCenter()
            }
            regen()
            if (text("公众号关注推荐").exists()) {
                var t = text("公众号关注推荐").findOnce().parent().child(5)
                if (t.text() == "开始活动") {
                    toastLog("公众号关注推荐")
                    var like = text("公众号关注推荐").findOnce().parent().child(5)
                    randomClick(like);
                    text("长按识别前往关注").findOne(20000)
                    sleep(random(3000, 5000))
                    if (!text("长按识别前往关注").exists()) {
                        toastLog("公众号关注推荐1")
                        var like = text("公众号关注推荐").findOnce().parent().child(5)
                        randomClick(like);
                        sleep(random(3000, 5000))
                        if (!text("长按识别前往关注").exists()) {
                            toastLog("公众号关注推荐2")
                            var like = text("公众号关注推荐").findOnce().parent().child(5)
                            randomClick(like);
                            sleep(random(3000, 5000))
                        }
                    }

                    if (text("长按识别前往关注").exists()) {
                        var n = text("长按识别前往关注").findOnce().indexInParent()
                        var like = text("长按识别前往关注").findOnce().parent().child((n - 1)).child(0)
                        longCk(like)
                        className("android.widget.LinearLayout").clickable(true).findOne(20000)
                        sleep(random(3000, 5000))

                        // text("前往图中包含的公众号")
                        if (className("android.widget.LinearLayout").clickable(true).exists()) {
                            var n = className("android.widget.LinearLayout").clickable(true).find().size()
                            var like = className("android.widget.LinearLayout").clickable(true).findOnce((n - 2))
                            randomClick(like)
                            sleep(random(3000, 5000))

                            text("关注公众号").findOne(20000)
                            sleep(random(3000, 5000))

                            // text("继续访问")text("非微信官方网页，请确认是否继续访问。")
                            if (text("继续访问").exists()) {
                                sleep(random(10000, 15000))
                                var like = text("继续访问").findOnce()
                                randomClick(like);
                                sleep(random(3000, 5000))
                            }
                            // 微信受限，被封控
                            if (text("未关注").exists()) {
                                toastLog("XX公众号未打开，可能被限制了XX")
                                var like = text("未关注").findOnce()
                                randomClick(like);
                                sleep(random(3000, 5000))
                                break
                            }

                            upsm(1)

                            // text("关注公众号")
                            if (className("android.widget.Button").exists()) {
                                toastLog("发现公众号")
                                var like = className("android.widget.Button").findOnce()
                                randomClick(like);
                                sleep(random(3000, 5000))
                                sleep(random(3000, 5000))
                            }

                            for (var iii = 0; iii < 4; iii++) {
                                // text("关注公众号")
                                if (text("本次已关注").exists()) {
                                    toastLog("本次已关注")
                                    var like = text("本次已关注").findOnce()
                                    randomClick(like);
                                    sleep(random(3000, 5000))
                                    sleep(random(3000, 5000))
                                    break
                                } else {
                                    back()
                                    sleep(random(3000, 5000))
                                    sleep(random(3000, 5000))
                                }
                            }
                            if (text("本次已关注").exists()) {
                                toastLog("重新确认已经关注")
                                var like = text("本次已关注").findOnce()
                                randomClick(like);
                                sleep(random(3000, 5000))
                                sleep(random(3000, 5000))
                            }
                            text("互助活动").findOne(20000)
                            sleep(random(3000, 5000))

                            // text("已停止访问该网页")
                            if (text("已停止访问该网页").exists()) {
                                var like = desc("返回").findOnce()
                                randomClick(like);
                                sleep(random(3000, 5000))
                                taskCenter()
                            }
                        } else {
                            if (text("转发给朋友").exists()) {
                                toastLog("没找到-长按识别")
                                var like = text("取消").findOnce()
                                randomClick(like);
                                sleep(random(3000, 5000))
                                regen()
                            }
                        }

                    } else {
                        if (t.text() == "开始活动") {
                            toastLog("公众号关注-可能没有任务了")
                            sleep(random(3000, 5000))
                            break
                        }
                    }
                } else {
                    toastLog("公众号关注推荐" + t.text())
                    sleep(random(3000, 5000))
                    break
                }
            }
            toastLog("第-" + (i + 1) + "-次公众号关注完成")
            sleep(random(3000, 5000))
        } catch (error) {
            toastLog("公众号关注异常错误,未完成")
            toastLog(error)
            sleep(random(3000, 5000))
        }
    }
}

// text("视频号关注推荐")
function vilook() {
    if (text("视频号关注推荐").exists()) {
        for (var i = 0; i < 10; i++) {
            try {
                for (var ii = 0; ii < 2; ii++) {
                    // "关注未点中"
                    if (text("本次已关注").exists()) {
                        toastLog("发现关注视频号按钮")
                        var like = text("本次已关注").findOnce()
                        randomClick(like);
                        sleep(random(3000, 5000))
                        sleep(random(3000, 5000))
                        break
                    }
                    if (!text("互助活动").exists()) {
                        toastLog("未发现互助活动，返回")
                        back()
                        sleep(random(3000, 5000))
                        sleep(random(3000, 5000))
                    }
                    if (text("互助活动").exists()) {
                        break
                    }
                }
                if (!text("视频号关注推荐").exists()) {
                    var like = text("互助活动").findOnce().parent()
                    randomClick(like)
                    sleep(random(3000, 5000))
                }
                if (!text("视频号关注推荐").exists()) {
                    taskCenter()
                }

                regen()
                var t = text("视频号关注推荐").findOnce().parent().child(5)
                if (t.bounds().bottom > 1800) {
                    upsm(1)
                }
                if (t.text() == "开始活动") {
                    toastLog("视频号关注推荐")
                    var like = text("视频号关注推荐").findOnce().parent().child(5)
                    randomClick(like);
                    text("长按识别前往关注").findOne(20000)
                    sleep(random(3000, 5000))
                    if (!text("长按识别前往关注").exists()) {
                        toastLog("视频号关注推荐1")
                        var like = text("视频号关注推荐").findOnce().parent().child(6)
                        randomClick(like);
                        sleep(random(3000, 5000))
                        if (!text("长按识别前往关注").exists()) {
                            toastLog("视频号关注推荐2")
                            var like = text("视频号关注推荐").findOnce().parent().child(5)
                            randomClick(like);
                            sleep(random(3000, 5000))
                        }
                    }

                    if (text("长按识别前往关注").exists()) {
                        // var like = text("长按识别前往关注").findOnce().parent().child(7).child(0)
                        var n = text("长按识别前往关注").findOnce().indexInParent()
                        var like = text("长按识别前往关注").findOnce().parent().child((n - 1)).child(0)
                        longCk(like)
                        text("前往图中包含的视频号").findOne(20000)
                        sleep(random(3000, 5000))
                        // text("前往图中包含的视频号")
                        if (className("android.widget.LinearLayout").clickable(true).exists()) {
                            var n = className("android.widget.LinearLayout").clickable(true).find().size()
                            var like = className("android.widget.LinearLayout").clickable(true).findOnce((n - 2))
                            randomClick(like)
                            sleep(random(3000, 5000))
                            sleep(random(3000, 5000))
                            if (text("前往图中包含的视频号").exists()) {
                                click(randX, randY)
                                sleep(random(3000, 5000))
                                sleep(random(3000, 5000))
                            }
                            id("fyk").findOne(20000)
                            sleep(random(3000, 5000))
                            // 微信受限，被封控
                            if (text("本次未关注").exists()) {
                                toastLog("XX视频号未打开，可能被限制了XX")
                                var like = text("本次未关注").findOnce()
                                randomClick(like);
                                sleep(random(3000, 5000))
                                break
                            }
                            // text("全部直播预告(3)")
                            if (textStartsWith("全部直播预告").exists()) {
                                back()
                                sleep(random(3000, 5000))
                            }

                            // // text("视频")bounds("(62,1011,138,1055)")
                            // var t = text("视频").findOnce()
                            // if (t.bounds().bottom > 1000) {
                            //     upsm(1)
                            // }

                            // g观看视频，退出id//("com.tencent.mm:id/l9_")小心心
                            if (id("com.tencent.mm:id/l9_").exists()) {
                                toastLog("观看视频")
                                if (id("com.tencent.mm:id/l9_").findOnce().bounds().bottom > 1800) {
                                    upsm(1)
                                }

                                var like = id("com.tencent.mm:id/l9_").findOnce().parent().parent().parent().parent().parent()
                                randomClick(like)
                                sleep((18000, 25000))
                                for (var iii = 0; iii < 2; iii++) {
                                    sml_move(device.width / 2, device.height * 0.8, device.width / 2, device.height * 0.2, 500);
                                    sleep((18000, 25000));
                                }

                                back()
                                sleep(random(3000, 5000))
                                dosm(1)
                            }

                            if (id("fyk").exists()) {
                                toastLog("关注")
                                var like = id("fyk").findOnce()
                                randomClick(like)
                                sleep(random(3000, 5000))

                            }
                            text("已关注").findOne(10000)
                            sleep(random(3000, 5000))

                            if (textStartsWith("本次操作存在安全风险暂未生效").exists()) {
                                toastLog("存在安全风")
                                var like = text("确定").findOnce()
                                randomClick(like)
                                sleep(random(3000, 5000))
                                back()
                                sleep(random(3000, 5000))
                                text("未关注").findOne(20000)
                                sleep(random(3000, 5000))
                                if (text("未关注").exists()) {
                                    var like = text("未关注").findOnce()
                                    randomClick(like)
                                    sleep(random(3000, 5000))
                                }
                                break

                                // text("操作太频繁，请稍后再试")
                            } else if (text("操作太频繁，请稍后再试").exists()) {
                                toastLog("操作太频繁")
                                var like = text("确定").findOnce()
                                randomClick(like)
                                sleep(random(3000, 5000))
                                back()
                                sleep(random(3000, 5000))
                                text("未关注").findOne(20000)
                                sleep(random(3000, 5000))
                                if (text("未关注").exists()) {
                                    var like = text("未关注").findOnce()
                                    randomClick(like)
                                    sleep(random(3000, 5000))
                                }
                                break
                            } else if (text("本次操作由于行为异常被拦截，详情见微信团队通知。").exists()) {
                                var like = text("确定").findOnce()
                                randomClick(like)
                                toastLog("行为异常被拦截")
                                sleep(random(3000, 5000))
                                back()
                                sleep(random(3000, 5000))
                                text("未关注").findOne(20000)
                                sleep(random(3000, 5000))
                                if (text("未关注").exists()) {
                                    var like = text("未关注").findOnce()
                                    randomClick(like)
                                    sleep(random(3000, 5000))
                                }
                                break
                            } else {
                                let maxRetry = 5;
                                while (!text("本次已关注").exists() && maxRetry-- > 0) {
                                    toastLog("寻找本次已关注")
                                    back()
                                    sleep(random(3000, 5000))
                                }
                                if (text("本次已关注").exists()) {
                                    var like = text("本次已关注").findOnce()
                                    randomClick(like)
                                    sleep(random(3000, 5000))
                                }
                            }
                        }
                    } else {
                        if (t.text() == "开始活动") {
                            toastLog("视频号关注-可能没有任务了")
                            sleep(random(3000, 5000))
                            break
                        }
                    }
                } else {
                    toastLog("视频号关注推荐" + t.text())
                    sleep(random(3000, 5000))
                    break
                }

                toastLog("第-" + (i + 1) + "-次视频号关注完成")
                sleep(random(3000, 5000))

            } catch (error) {
                toastLog("视频号关注异常错误,未完成")
                sleep(random(3000, 5000))
            }
        }
    }
}

// 提现  积分转移idh,提现到微信密码mm,提现金额money
function tx(idh, mm, money) {
    try {
        toastLog("开始提现任务")
        if (text("互助活动").exists()) {
            var like = text("互助活动").findOnce().parent()
            randomClick(like)
            text("文章阅读推荐").findOne(10000)
            sleep(random(3000, 5000))
        }

        var t = textStartsWith("可兑换").findOnce()
        if (t.bounds().bottom < 270) {
            dosm(1)
        }
        if (textStartsWith("可兑换:").exists()) {
            var like = textStartsWith("可兑换:").findOnce().text()
            toastLog(textStartsWith("可用积分").findOnce().text())
            sleep(random(3000, 5000))
            if (like.match(/\d+/g) >= money) {
                toastLog("兑换积分")
                var like = textStartsWith("兑换").findOnce()
                randomClick(like)
                sleep(random(3000, 5000))

                // // text("为保障资金安全，请设置提现密码")
                // if (text("为保障资金安全，请设置提现密码").exists()) {
                //     toastLog("设置提现密码")
                //     var like = text("创建密码(4位数字即可，建议手机号后4位)").findOnce()
                //     randomClick(like)
                //     sleep(random(3000, 5000))
                //     setClip(mm)
                //     sleep(random(2000, 3000))
                //     like.paste()
                //     sleep(random(2000, 3000))

                //     if (text("请再次输入，以便确认").exists()) {
                //         toastLog("再次输入提现密码")
                //         var like = text("请再次输入，以便确认").findOnce()
                //         randomClick(like)
                //         sleep(random(3000, 5000))
                //         setClip(mm)
                //         sleep(random(2000, 3000))
                //         like.paste()
                //         sleep(random(2000, 3000))
                //     }
                //     if (text("确认创建密码").exists()) {
                //         toastLog("确认密码")
                //         var like = text("确认创建密码").findOnce()
                //         randomClick(like)
                //         sleep(random(3000, 5000))
                //     }
                //     if (textStartsWith("兑换").exists()) {
                //         var like = textStartsWith("兑换").findOnce()
                //         randomClick(like)
                //         sleep(random(3000, 5000))
                //     }
                // }

                var n = textStartsWith("用户ID").findOnce().indexInParent()
                var username = textStartsWith("用户ID").findOnce().parent().child((n - 1)).text().match(/\d+/g)

                // text("积分转移")
                if (username == null) {
                    toastLog("积分转移")
                    var like = text("积分转移").findOnce()
                    randomClick(like)
                    sleep(random(3000, 5000))

                    // 绑定积分转移对象
                    var like1 = text("转移对象").findOnce(0).indexInParent()
                    var like = text("转移对象").findOnce(0).parent().child((like1 + 1)).text()
                    if (like == null) {
                        toastLog("绑定积分转移对象")
                        var like = text("请输入对方ID").findOnce()
                        randomClick(like)
                        sleep(random(3000, 5000))
                        setClip(idh)
                        sleep(random(2000, 3000))
                        like.paste()
                        sleep(random(3000, 5000))
                        if (text("查询昵称").exists()) {
                            var like = text("查询昵称").findOnce()
                            randomClick(like)
                            sleep(random(3000, 5000))
                        }
                        if (text("确认绑定").exists()) {
                            var like = text("确认绑定").findOnce()
                            randomClick(like)
                            sleep(random(3000, 5000))
                        }
                        // 绑定完成，继续转移
                        if (text("用户提现").exists()) {
                            var like = text("积分转移").findOnce()
                            randomClick(like)
                            sleep(random(3000, 5000))
                        }
                    }
                    // text("积分转移")
                    if (text("可转移的积分").exists()) {
                        toastLog("积分转移1")
                        var like = text("可转移的积分").findOnce().parent().child(7)
                        randomClick(like)
                        sleep(random(3000, 5000))
                        if (text("积分安全验证").exists()) {
                            var userid = textStartsWith("用户ID").findOnce().text().match(/\d+/g)[0]
                            if (text("输入用户ID").exists()) {
                                var like = text("输入用户ID").findOnce()
                                randomClick(like)
                                sleep(random(2000, 3000))
                                setClip(userid)
                                sleep(random(2000, 3000))
                                like.paste()
                                sleep(random(2000, 3000))
                            }
                            if (text("输入提现密码").exists()) {
                                var like = text("输入提现密码").findOnce()
                                randomClick(like)
                                sleep(random(2000, 3000))
                                setClip(mm)
                                sleep(random(2000, 3000))
                                like.paste()
                                sleep(random(2000, 3000))
                            }
                            if (text("确认").exists()) {
                                var like = text("确认").findOnce()
                                randomClick(like)
                                sleep(random(3000, 5000))
                                sleep(random(3000, 5000))
                            }
                        }
                    }
                    toastLog("目前" + textStartsWith("可用积分").findOnce().text())
                    sleep(random(3000, 5000))
                } else {
                    // text("提现到微信")
                    toastLog("提现到微信")
                    var like = text("提现到微信").findOnce()
                    randomClick(like)
                    sleep(random(3000, 5000))
                    // 输入id 密码8888 确定提现
                    if (text("积分安全验证").exists()) {
                        var userid = textStartsWith("用户ID").findOnce().text().match(/\d+/g)[0]
                        if (text("输入用户ID").exists()) {
                            var like = text("输入用户ID").findOnce()
                            randomClick(like)
                            sleep(random(2000, 3000))
                            setClip(userid)
                            sleep(random(2000, 3000))
                            like.paste()
                            sleep(random(2000, 3000))
                        }
                        if (text("输入提现密码").exists()) {
                            var like = text("输入提现密码").findOnce()
                            randomClick(like)
                            sleep(random(2000, 3000))
                            setClip(mm)
                            sleep(random(2000, 3000))
                            like.paste()
                            sleep(random(2000, 3000))
                        }

                        // 确定提现
                        if (text("确认").exists()) {
                            var like = text("确认").findOnce()
                            randomClick(like)
                            sleep(random(3000, 5000))
                            sleep(random(3000, 5000))
                        }
                        toastLog("目前" + textStartsWith("可用积分").findOnce().text())
                        sleep(random(3000, 5000))
                    }
                }
                regen()
            } else {
                toastLog("未达到设定最低提现金额")
                sleep(random(3000, 5000))
            }
        }
    } catch (error) {
        toastLog("提现异常错误,未完成")
        sleep(random(3000, 5000))
    }
}

// 视频号浏览
function vi(n) {
    try {
        for (var i = 0; i < 5; i++) {

            // desc("返回")
            if (text("互助活动").exists()) {
                toastLog("返回微信主页")
                var like = clickable(true).boundsInside(0, 0, 200, 300).findOnce()
                randomClick(like)
                sleep(random(3000, 5000))
                sleep(random(3000, 5000))
            }
            if (!id("nvt").exists()) {
                back()
                sleep(random(3000, 5000))
            } else {
                break
            }
        }
        // //我
        if (id("nvt").exists()) {
            toastLog("发现微信主页面")
            var like = id("nvt").findOnce(2)
            randomClick(like);
            sleep(random(3000, 5000))
            if (desc("视频号").exists()) {
                toastLog("开始浏览视频")
                var like = desc("视频号").findOnce()
                randomClick(like);
                text("设置青少年模式").findOne(10000)
                sleep(random(3000, 5000))
                // text("设置青少年模式")

                if (className("android.widget.Button").exists()) {
                    toastLog("设置青少年模式")
                    var like = className("android.widget.Button").findOnce()
                    randomClick(like)
                    sleep(random(3000, 5000))
                }
                for (var i = 0; i < n; i++) {
                    sml_move(device.width / 2, device.height * 0.8, device.width / 2, device.height * 0.2, 500);
                    sleep(random(15000, 25000));
                }
                toastLog("视频浏览结束")
                back()
                sleep(random(3000, 5000))
            }
        }
    } catch (error) {
        toastLog("视频号浏览异常错误,未完成")
        sleep(random(3000, 5000))
    }
}

// 订阅号浏览
function read(n) {

    // //我
    for (var i = 0; i < 5; i++) {
        // desc("返回")
        if (text("互助活动").exists()) {
            toastLog("返回微信主页")
            var like = clickable(true).boundsInside(0, 0, 200, 300).findOnce()
            randomClick(like)
            sleep(random(3000, 5000))
            sleep(random(3000, 5000))
        }
        if (!id("nvt").exists()) {
            back()
            sleep(random(3000, 5000))
        } else {
            break
        }
    }
    try {
        if (id("nvt").exists()) {
            toastLog("发现微信主页面")
            var like = id("nvt").findOne(0)
            randomClick(like);
            sleep(random(3000, 5000))

            var like = id("meb").findOnce()
            randomClick(like);
            sleep(random(3000, 5000))

            // className("android.widget.EditText")id("d98")
            if (className("android.widget.EditText").exists()) {
                setText("订阅号消息")
                sleep(random(3000, 5000))
                if (text("订阅号消息").findOnce(1) == null) {
                    toastLog("搜索框没发现")
                    var like = text("订阅号消息").clickable(true).findOnce()
                    randomClick(like);
                    sleep(random(3000, 5000))
                }
                // 订阅号消息列表
                if (text("订阅号消息").exists()) {
                    toastLog("订阅号浏览")
                    var like = textStartsWith("订阅号消息").findOnce(1)
                    randomClick(like);
                    text(" - 功能").findOne(20000)
                    sleep(random(3000, 5000))
                }
                // text("功能")
                // if (text("订阅号消息 查看订阅号推送的文章、通知等 前往").exists()) {
                //     toastLog("订阅号功能")
                //     var like = text("订阅号消息 查看订阅号推送的文章、通知等 前往").findOnce()
                //     randomClick(like);
                //     sleep(random(3000, 5000))
                // }
                if (text(" - 功能").exists()) {
                    toastLog("订阅号功能")
                    var like = text(" - 功能").findOnce().parent().parent().parent().parent().child(1)
                    randomClick(like);
                    sleep(random(3000, 5000))
                    if (text(" - 功能").exists()) {
                        toastLog("订阅号功能")
                        var like = text(" - 功能").findOnce().parent().parent().parent().parent().child(1)
                        randomClick(like);
                        sleep(random(3000, 5000))
                        if (text(" - 功能").exists()) {
                            toastLog("订阅号功能")
                            var like = text(" - 功能").findOnce().parent().parent().parent().parent().child(1)
                            randomClick(like);
                            sleep(random(3000, 5000))
                        }
                    }
                }
                for (var i = 0; i < 5; i++) {
                    upsm(1)
                    toastLog("浏览订阅号文章")
                    // className("android.widget.Button")
                    var like1 = className("android.widget.Button").visibleToUser().findOnce(4).parent().childCount()
                    var like = className("android.widget.Button").visibleToUser().findOnce(4).parent().child((like1 - 1))
                    // randomClick(like);
                    var randX = random(200, like.bounds().right - 1);
                    var randY = random(like.bounds().top + 1, like.bounds().bottom - 1);
                    click(randX, randY)
                    sleep(random(3000, 5000))
                    upsm(3)
                    // desc("返回")className("android.widget.Button")
                    // desc("菜单")className("android.widget.Button")
                    // desc("搜索")className("android.widget.Button")
                    while (!desc("菜单").exists()) {
                        toastLog("未发现公众号")
                        var mypk = currentPackage()
                        if (mypk != pn) {
                            toastLog("已经跳出微信")
                            break
                        }
                        back()
                        sleep(random(3000, 5000))
                    }
                    sleep(random(3000, 5000));
                }

                toastLog("浏览结束")

                while (!id("nvt").exists()) {
                    toastLog("未发现通讯录")
                    var mypk = currentPackage()
                    if (mypk != pn) {
                        toastLog("已经跳出微信")
                        break
                    }
                    back()
                    sleep(random(3000, 5000))
                }
                sleep(random(3000, 5000))
            }
        }
    } catch (error) {
        toastLog("订阅号浏览异常错误,未完成")
        toastLog(error)
        sleep(random(3000, 5000))
    }
}

//退出app
function exapp() {
    // // 停止所有通过threads.start()启动的子线程。
    threads.shutDownAll()

    // desc("返回")
    if (text("互助活动").exists()) {
        toastLog("返回微信主页")
        var like = desc("更多信息").findOnce().parent().parent().parent().parent().parent().child(1)
        randomClick(like)
        sleep(random(3000, 5000))
        sleep(random(3000, 5000))
    }
    back()
    sleep(random(3000, 5000))
    back()
    sleep(random(3000, 5000))
    back()
    sleep(random(400, 600))
    back()
    toastLog("任务全部完成,退出===微信===")
}












function regen() {
    for (var iii = 0; iii < 1; iii++) {
        if (className("android.widget.ImageView").clickable(true).exists()) {
            var like = className("android.widget.ImageView").clickable(true).findOnce()
            randomClick(like)
            sleep(random(3000, 5000))
            // text("刷新")
            if (className("androidx.recyclerview.widget.RecyclerView").exists()) {
                var n = className("androidx.recyclerview.widget.RecyclerView").findOnce(0).childCount()
                if (n == 0) {
                    var n = 3
                } else {
                    var n = 4
                }
                var like1 = className("androidx.recyclerview.widget.RecyclerView").find().size()
                var like = className("androidx.recyclerview.widget.RecyclerView").findOnce((like1 - 1)).child(n)
                randomClick(like)
                text("互助活动").findOne(10000)
                sleep(random(3000, 5000))
                sleep(random(3000, 5000))
            }
        }
    }
}


// //上滑2次
function upsm(x) {
    for (var c = 0; c < x; c++) {
        // 设置起始点的坐标
        var startX = random(device.width * 0.25, device.width * 0.75)
        var startY = random(device.height * 0.75, device.height * 0.8)
        // 设置结束点的坐标
        var endX = random(device.width * 0.25, device.width * 0.75)
        var endY = random(device.height * 0.25, device.height * 0.35)

        sml_move(startX, startY, endX, endY, 500);
        sleep(random(3000, 5000));
    }
    toastLog("上滑" + x + "次")
}

// //下滑2次
function dosm(x) {
    for (var c = 0; c < x; c++) {
        // 设置起始点的坐标
        var startX = random(device.width * 0.25, device.width * 0.75)
        var startY = random(device.height * 0.25, device.height * 0.35)
        // 设置结束点的坐标
        var endX = random(device.width * 0.25, device.width * 0.75)
        var endY = random(device.height * 0.75, device.height * 0.85)

        sml_move(startX, startY, endX, endY, 500);
        sleep(random(3000, 5000));
    }
    toastLog("下滑" + x + "次")
}


//长按
function longCk(kjobj) {
    var bound = kjobj.bounds();
    var randX = bound.left + random(1, bound.right - bound.left - 2);
    var randY = bound.top + random(1, bound.bottom - bound.top - 2);
    longClick(randX, randY)
}

// 控件-随机点击
function randomClick(kjobj) {
    var bound = kjobj.bounds();
    // var randX = bound.left + random(1, bound.right - bound.left - 2);
    // var randY = bound.top + random(1, bound.bottom - bound.top - 2);

    var randX = random(bound.left + 1, bound.right - 1);
    var randY = random(bound.top + 1, bound.bottom - 1);
    click(randX, randY)//(0, 1904 - 216, 2040)
}

// 查找控件-文字识别-识别范围查找控件-识别范围坐标点击
// click_ocr("互助活动", className("android.widget.TextView"), ".parent()", 0, 0, 0, device.height * 0.2, device.width * 0.5, device.height)
function click_ocr(str, obj, par, mo1, mo2, x1, y1, x2, y2) {
    var like1 = text(str).visibleToUser().findOnce();
    if (like1) {
        toastLog("已经找到-" + str + "，开始点击");
        var like = eval("like1" + par);
        randomClick(like);
        sleep(random(3000, 5000));
        return true;
    } else {
        toastLog("未找到-" + str + "，开始识别");
        if (!requestScreenCapture()) {
            log("请求截图权限失败");
            exit();
        }
        var savePath = "/sdcard/Download/croppedpng.png";
        var screen = captureScreen();
        var croppedpng = images.clip(screen, x1, y1, x2 - x1, y2 - y1);
        images.save(croppedpng, savePath);
        var img = images.read(savePath);
        var useSlim = false;
        var arr = paddle.ocr(img, useSlim);
        var found = false;
        for (var i = 0; i < arr.length; i++) {
            if (arr[i].text == str) {
                found = true;
                var x = random(x1 + arr[i].bounds.left, x1 + arr[i].bounds.right);
                var y = random(y1 + arr[i].bounds.top, y1 + arr[i].bounds.bottom);
                var like1 = obj.boundsInside(x1 + arr[i].bounds.left - 10, y1 + arr[i].bounds.top - 10, x1 + arr[i].bounds.right + 10, y1 + arr[i].bounds.bottom + 10).findOnce();
                if (like1) {
                    toastLog("找到对应控件，开始点击");
                    var like = eval("like1" + par);
                    randomClick(like);
                    sleep(random(3000, 5000));
                    return true;
                } else {
                    toastLog("未找到控件，直接点击坐标");
                    click(x + mo1, y + mo2);
                    sleep(random(3000, 5000));
                    return true;
                }
            }
        }
        if (!found) {
            toastLog("未识别到：" + str);
            sleep(random(3000, 5000));
        }
        img.recycle();
        return false;
    }
}


/**
 * 贝塞尔曲线
 * @param {坐标点} ScreenPoint 
 * @param {偏移量} Offset 
 */
function bezier_curves(ScreenPoint, Offset) {
    cx = 3.0 * (ScreenPoint[1].x - ScreenPoint[0].x);
    bx = 3.0 * (ScreenPoint[2].x - ScreenPoint[1].x) - cx;
    ax = ScreenPoint[3].x - ScreenPoint[0].x - cx - bx;
    cy = 3.0 * (ScreenPoint[1].y - ScreenPoint[0].y);
    by = 3.0 * (ScreenPoint[2].y - ScreenPoint[1].y) - cy;
    ay = ScreenPoint[3].y - ScreenPoint[0].y - cy - by;
    tSquared = Offset * Offset;
    tCubed = tSquared * Offset;
    result = {
        "x": 0,
        "y": 0
    };
    result.x = (ax * tCubed) + (bx * tSquared) + (cx * Offset) + ScreenPoint[0].x;
    result.y = (ay * tCubed) + (by * tSquared) + (cy * Offset) + ScreenPoint[0].y;
    return result;
};


/**
 * 曲线滑动
 * @param {*} qx 
 * @param {*} qy 
 * @param {*} zx 
 * @param {*} zy 
 * @param {*} time 
 */
function sml_move(qx, qy, zx, zy, time) {
    var xxy = [time];
    var point = [];
    var dx0 = {
        "x": qx,
        "y": qy
    };
    var dx1 = {
        "x": random(qx - 100, qx + 100),
        "y": random(qy, qy + 50)
    };
    var dx2 = {
        "x": random(zx - 100, zx + 100),
        "y": random(zy, zy + 50),
    };
    var dx3 = {
        "x": zx,
        "y": zy
    };
    for (var i = 0; i < 4; i++) {
        eval("point.push(dx" + i + ")");
    };
    for (let i = 0; i < 1; i += 0.08) {
        let newPoint = bezier_curves(point, i);
        xxyy = [parseInt(newPoint.x), parseInt(newPoint.y)]
        xxy.push(xxyy);
    }
    gesture.apply(null, xxy);
};
